package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolCompletionForManagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.DailyFeedBackForEngineeringInstallAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.PatrolCompletionOrderForManagerAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;

import com.cloudsigns.factorymanager2.engineering.presenter.PatrolCompletionForManagePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by tony on 2018/6/18.
 */

public class PatrolCompletionOrderForManagerActivity extends Activity implements View.OnClickListener, ItemClickListener, IPatrolOrderDetailView, IPatrolCompletionForManagerView {
    private TextView tv_title;
    private ViewGroup rl_back;
    private final String TAG = PatrolCompletionOrderForManagerActivity.class.getSimpleName();
    private TextView tv_region_name;
    private ListView lv;
    private TextView edt_handle_account, ed_handle_date;
    private TextView btn_confirm_evaluation;

    private PatrolCompletionOrderForManagerAdapter adapter;
    private List<MediaBean> mediaBeanList = new ArrayList<>();
    private HashMap<String, ArrayList<MediaBean>> dataMap = new HashMap<>(); //id
    private HashMap<String, Boolean> isSucessMap = new HashMap<>();
    private OrderParentBean orderParentBean;
    private String orderId = "";
    private PatrolOrderDetailPresenter patrolOrderDetailPresenter;
    private PatrolCompletionForManagePresenter patrolCompletionForManagePresenter;
    private PatrolOrderBean patrolOrderBean;
    private boolean isCLick;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_patrol_completion_order_for_manager);

        patrolOrderDetailPresenter = new PatrolOrderDetailPresenter(this);
        patrolCompletionForManagePresenter = new PatrolCompletionForManagePresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);

            if (null != orderParentBean) {
                orderId = orderParentBean.getId();
                tv_region_name.setText(orderParentBean.getProjectName());
            }
        }

        patrolOrderDetailPresenter.getPatrolOrderDetail();
    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        tv_region_name = findViewById(R.id.tv_region_name);

        edt_handle_account = findViewById(R.id.edt_handle_account);
        ed_handle_date = findViewById(R.id.ed_handle_date);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);

        lv = findViewById(R.id.lv);

        adapter = new PatrolCompletionOrderForManagerAdapter(this, TAG);
        adapter.setData(mediaBeanList);
        btn_confirm_evaluation.setOnClickListener(this);
        adapter.setItemClick(this);
        lv.setAdapter(adapter);

        tv_title.setText("巡查");

        rl_back.setOnClickListener(this);

    }

    private void initData() {
        if (null != patrolOrderBean) {
            tv_region_name.setText(patrolOrderBean.getRegionName());

            dataMap.clear();
            mediaBeanList.clear();
            isSucessMap.clear();

            CommonEnum.CompleteStatus completeStatus = patrolOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClicable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClicable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClicable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClicable(false);
                }
            }


            List<MediaBean> beanList = patrolOrderBean.getMediaList();
            if (null != beanList) {
                for (int i = 0; i < beanList.size(); i++) {
                    MediaBean mediaBean = beanList.get(i);
                    String assignReceiveOrderId = mediaBean.getAssignReceiveOrderId();

                    CommonEnum.CompleteStatus assignCompleteStatus = mediaBean.getAssignCompleteStatus();
                    if (null != assignCompleteStatus) {
                        if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKING) {
                            isSucessMap.put(assignReceiveOrderId, false);
                        } else if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                            isSucessMap.put(assignReceiveOrderId, false);
                        } else if (assignCompleteStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                            isSucessMap.put(assignReceiveOrderId, true);
                        } else if (assignCompleteStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                            isSucessMap.put(assignReceiveOrderId, true);
                        }
                    }

                    if (dataMap.containsKey(assignReceiveOrderId)) {
                        dataMap.get(assignReceiveOrderId).add(mediaBean);
                    } else {
                        ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                        mediaBeanList1.add(mediaBean);
                        dataMap.put(assignReceiveOrderId, mediaBeanList1);
                        mediaBean.setAllMediaSuccess(isSucessMap.get(assignReceiveOrderId));
                        mediaBeanList.add(mediaBean);
                    }

                }
                adapter.setData(mediaBeanList);
                adapter.notifyDataSetChanged();
            }
        }
    }

    private void btnClicable(boolean bool) {
        isCLick = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void submit() {
        patrolCompletionForManagePresenter.patrolCompletionForManager();
    }

    ;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_confirm_evaluation:
                if (isCLick)
                    submit();
                break;

        }
    }


    @Override
    public void itemClick(View view, int position) {
        MediaBean mediaBean = (MediaBean) view.getTag();
        Intent mIntent = new Intent(PatrolCompletionOrderForManagerActivity.this, PatrolCompletionOrderSubmitForManagerActivity.class);
        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
        mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
        mIntent.putExtra("PatrolOrderBean", patrolOrderBean);
        mIntent.putExtra("mediaSize", dataMap.get(mediaBean.getAssignReceiveOrderId()).size());
        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    if (null != data)
                        patrolOrderDetailPresenter.getPatrolOrderDetail();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public String getOrderId() {

        return orderId;
    }

    @Override
    public void getPatrolOrderDetailSuccessed(PatrolOrderBean orderBean) {
        this.patrolOrderBean = orderBean;
        initData();
    }

    @Override
    public void gPatrolOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void patrolCompletionForManagerSuccess() {
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void patrolCompletionForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
